package com.zc58s.springbootmqtt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * api文档
 * </p>
 *
 * @author fujc-dev@qq.com
 */
@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger.enable}")
public class Swagger2Config {

    @Value("${swagger.enable}")
    private boolean enable;

    @Bean
    public Docket createAccepterRestApi() {

        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> globalParameters = new ArrayList<>();
        parameterBuilder.name("Authorization").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        globalParameters.add(parameterBuilder.build());

        globalParameters.add(new ParameterBuilder()
                .name("app")
                .description("前端应用")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("SpringBoot-MQTT")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zc58s"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .enable(enable)
                .globalOperationParameters(globalParameters);

    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Microservice-MQTT API")
                .description("SpringBoot学习记录Api文档")
                .version("1.0.0")
                .build();
    }
}